package com.manor.main.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.fastjson.JSON
import com.bumptech.glide.Glide
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.manor.common.http.HttpCallback
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.ToastUtil
import com.manor.main.R
import com.manor.main.activity.UserCenterActivity
import com.manor.main.adapter.VulgarItemAdapter
import com.manor.main.bean.ListBean
import com.manor.main.http.MainHttpConsts
import com.manor.main.http.MainHttpUtil
import kotlinx.android.synthetic.main.fragment_vulgar.*
import kotlinx.android.synthetic.main.vulgar_head.view.*


/**
 * 排行榜
 */
class VulgarItemFragment(var type: String, var type1: Int) : Fragment() {


    protected var mAdapter: VulgarItemAdapter? = null
    internal var pageNum = 1
    private var is_one = 0;

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        var view:View = inflater.inflate(R.layout.fragment_vulgar, container, false)

        return view
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        rcv_list.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        mAdapter = VulgarItemAdapter(R.layout.item_main_list, myList)
        rcv_list.setAdapter(mAdapter)
        is_one = 0

        srl_city.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNum++
                monde()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {

                pageNum = 1
                monde()
            }
        })
        mAdapter?.setOnItemClickListener { adapter, view, position ->
            if (ClickUtil.canClick()) {
                mAdapter?.run {
                    val listBean: ListBean = getItem(position)
                    context?.let { UserCenterActivity.forward(it, listBean.uid) }

                }
            }
        }

    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if(isVisibleToUser){

            monde()
        }else{
            MainHttpUtil.cancel(MainHttpConsts.CONSUME_LIST)
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    var myList: MutableList<ListBean> = ArrayList()
    fun monde() {

        if (type1 == 0) {
            MainHttpUtil.profitList(type, pageNum,
                    object : HttpCallback() {
                        override fun onSuccess(code: Int, msg: String, info: String) {
                            if (code == 0&&null!=info) {
                                setData(code, msg, info)
                                srl_city?.setEnableLoadMore(true)
                                srl_city?.setEnableRefresh(true)
                            }
                            ToastUtil.show(msg)
                        }
                    })
        } else {
            MainHttpUtil.consumeList(type, pageNum,
                    object : HttpCallback() {
                        override fun onSuccess(code: Int, msg: String, info: String) {
                            if (code == 0&&null!=info) {
                                setData(code, msg, info)
                                srl_city.setEnableLoadMore(true)
                                srl_city.setEnableRefresh(true)
                            }
                            ToastUtil.show(msg)
                        }
                    })
        }

    }

    fun setData(code: Int, msg: String, info: String) {
        if (pageNum == 1) {
            myList.clear()
            if(null==tv_wzd){
                return
            }
            pb.visibility = View.GONE
            if (info.isEmpty()) {
                tv_wzd.visibility = View.VISIBLE
            } else {
                tv_wzd.visibility = View.GONE
            }

            val hv1: View? = LayoutInflater.from(activity).inflate(R.layout.vulgar_head, null, false)
            val listBean = JSON.parseArray(info, ListBean::class.java)
            if(listBean.size==0){
                tv_wzd.visibility = View.VISIBLE
                return
            }
            hv1?.run {
                for (i in listBean.indices) {
                    when (i) {
                        0 -> {
                            activity?.run {
                                Glide.with(this).load(listBean[i].avatarThumb).into(iv_logo2)
                            }

                            if (listBean[i] != null) {
                                if (listBean[i].sex == 1) {
                                    tv_age2.leftDrawable = context?.getDrawable(R.mipmap.gender_nan)
                                    hv1.ll_city_genderbg2.background = context?.getDrawable(R.drawable.shap_vulagr_age)
                                } else {
                                    tv_age2.leftDrawable = context?.getDrawable(R.mipmap.gender_nv)
                                    ll_city_genderbg2.background = context?.getDrawable(R.drawable.shap_vulagr_age)
                                }
                            }
                            tv_age2.setText(listBean.get(i).age)
                            tv_name2.setText(listBean[i].userNiceName)
                            tv_money2.setText(listBean[i].totalCoin.toString())
                            lr_two.visibility = View.VISIBLE
                            iv_logo2.setOnClickListener({ UserCenterActivity.forward(context, listBean[i].uid)  })
                            lr_two.setOnClickListener({ UserCenterActivity.forward(context, listBean[i].uid) })

                        }
                        1 -> {
                            activity?.run {
                                Glide.with(this).load(listBean[i].avatarThumb).into(iv_logo)
                            }

                            if (listBean[i] != null) {
                                if (listBean[i].sex == 1) {
                                    tv_age.leftDrawable = context?.getDrawable(R.mipmap.gender_nan)
                                    ll_city_genderbg.background = context?.getDrawable(R.drawable.shap_vulagr_age)
                                } else {
                                    tv_age.leftDrawable = context?.getDrawable(R.mipmap.gender_nv)
                                    ll_city_genderbg.background = context?.getDrawable(R.drawable.shap_vulagr_age)
                                }
                            }
                            tv_age.setText(listBean.get(i).age)
                            tv_name.setText(listBean[i].userNiceName)
                            tv_money.setText(listBean[i].totalCoin.toString())
                            rl.visibility = View.VISIBLE
                            iv_logo.setOnClickListener({ UserCenterActivity.forward(context, listBean[i].uid) })
                            rl.setOnClickListener({ UserCenterActivity.forward(context, listBean[i].uid) })
                        }
                        2 -> {
                            activity?.run {
                                Glide.with(this).load(listBean[i].avatarThumb).into(iv_logo1)
                            }

                            if (listBean[i] != null) {
                                if (listBean[i].sex == 1) {
                                    tv_age1.leftDrawable = context?.getDrawable(R.mipmap.gender_nan)
                                    ll_city_genderbg1.background = context?.getDrawable(R.drawable.shap_vulagr_age)
                                } else {
                                    tv_age1.leftDrawable = context?.getDrawable(R.mipmap.gender_nv)
                                    ll_city_genderbg1.background = context?.getDrawable(R.drawable.shap_vulagr_age)
                                }
                            }
                            tv_age1.setText(listBean.get(i).age)
                            tv_name1.setText(listBean[i].userNiceName)
                            tv_money1.setText(listBean[i].totalCoin.toString())
                            rl_one.visibility = View.VISIBLE
                            iv_logo1.setOnClickListener({ UserCenterActivity.forward(context, listBean[i].uid) })
                            rl_one.setOnClickListener({ UserCenterActivity.forward(context, listBean[i].uid) })
                        }
                        else -> {
                            myList.add(listBean[i])
                        }
                    }
                }
                if(is_one == 0){
                    mAdapter?.addHeaderView(hv1)
                    is_one = 1
                }

                mAdapter?.setNewInstance(myList)
                srl_city.finishRefresh()
            }
        } else {
            val listBean = JSON.parseArray(info, ListBean::class.java)
            // myList.addAll(listBean)
            mAdapter?.addData(listBean)
            srl_city.finishLoadMore()
        }
    }

    override fun onDestroy() {
        MainHttpUtil.cancel(MainHttpConsts.PROFIT_LIST)
        MainHttpUtil.cancel(MainHttpConsts.CONSUME_LIST)
        super.onDestroy()

    }

    override fun onResume() {
        super.onResume()

    }
}
